<?php


namespace app\zhongcan\controller;


use app\zhongcan\model\NewsModel;
use app\zhongcan\validate\NewsValidate;
use cmf\controller\AdminBaseController;
use think\facade\Db;


class NewsController extends AdminBaseController
{
    public function index()
    {
        // 获取搜索关键字
        $keyword = input('get.keyword', '');
        $where = [];
        if ($keyword) {
            $where[] = ['title|author', 'like', '%' . $keyword . '%'];
        }


        // 查询所有新闻并传递给视图
        $newsList = NewsModel::where($where)
            ->order("list_order", 'desc')
            ->paginate(2); // 每页显示2条记录
        foreach ($newsList as $news) {
            $content = $news['content'];
            $content1 = htmlspecialchars_decode($content);
            $content2 = cmf_replace_content_file_url($content1, true);
            $news['content'] = $content2;
        }

        $page = $newsList->render();
        $this->assign('newsList', $newsList);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function show($id)
    {
        $news = NewsModel::find($id);
        if($news['switch'] == 1){
            $news->switch = 0;
            if ($news->save()) {
                $this->success('编辑成功', url('News/index'));
            } else {
                $this->error('编辑失败');
            }
        }else{
            $news->switch = 1;
            if ($news->save()) {
                $this->success('编辑成功', url('News/index'));
            } else {
                $this->error('编辑失败');
            }
        }
    }

    public function add()
    {
        return $this->fetch('add');
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
                $data = input('post.');
//                halt($data['image']);
                $con1 = htmlspecialchars_decode($data['content']);
                $con2 = cmf_replace_content_file_url($con1,true);
                $con3 = htmlspecialchars($con2);
                $data['content'] = $con3;
                //验证数据
                $validate = new NewsValidate();
                $result = $validate->scene('add')->check($data);
                if (!$result) {
                    return json(['code' => 0, 'msg' => $validate->getError(), 'data' => null]);
                }
//                $data['create_time'] = date('Y-m-d H:i:s');
                $result = NewsModel::create($data);
                if ($result) {
                    $this->success('添加成功', url('News/index'));
                } else {
                    $this->error('添加失败');
                }
            }
    }

    public function edit($id)
    {
        $news = NewsModel::find($id);
        if (!$news) {
            $this->error('新闻不存在');
        }

        $content = $news['content'];
        $content1 = htmlspecialchars_decode($content);
        $content2 = cmf_replace_content_file_url($content1, true);
        $news['content'] = $content2;

        $this->assign('news', $news);

        return $this->fetch('edit');
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data = input('post.');

            //验证数据
            $validate = new NewsValidate();
            $result = $validate->scene('edit')->check($data);
            if (!$result) {
                return json(['code' => 0, 'msg' => $validate->getError(), 'data' => null]);
            }

            $news = NewsModel::find($data['id']);
            if (!$news) {
                $this->error('新闻不存在');
            }

            // 处理内容中的图片地址
            $con = $data['content'];
            $con1 = htmlspecialchars_decode($con);
            $con2 = cmf_replace_content_file_url($con1, true);
            $con3 = htmlspecialchars($con2);
            $data['content'] = $con3;

            //switch的默认值
            $data['switch'] = 1;

            $news->title = $data['title'];
            $news->image = $data['image'];
            $news->look_num = $data['look_num'];
            $news->author = $data['author'];
            $news->abstract = $data['abstract'];
            $news->content = $data['content'];
            $news->switch = $data['switch'];
            $news->update_time = date('Y-m-d H:i:s');
            if ($news->save()) {
                $this->success('编辑成功', url('News/index'));
            } else {
                $this->error('编辑失败');
            }
        }
    }


    public function delete($id)
    {
        $news = NewsModel::find($id);
        if (!$news) {
            $this->error('新闻不存在');
        }

        if ($news->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function order()
    {
        parent::listOrders(new NewsModel());
        $this->success('排序成功');
    }

    public function detali($id)
    {
        $news = NewsModel::find($id);
        if (!$news) {
            $this->error('新闻不存在');
        }

        $content = $news['content'];
        $content1 = htmlspecialchars_decode($content);
        $content2 = cmf_replace_content_file_url($content1, true);
        $news['content'] = $content2;

        $this->assign('news', $news);

        return $this->fetch('detali');

    }
}